package com.lechi.web.runner;

import com.lechi.web.job.BatchInsertJob;
import com.lechi.web.quartz.scheduler.DynamicJob;
import com.lechi.web.quartz.scheduler.DynamicSchedulerFactory;
import com.lechi.web.service.ISchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 项目启动后添加定时轮训任务
 */
@Slf4j
@Order(1)
@Component
public class BatchInsertSchedule implements CommandLineRunner {

    @Autowired
    private ISchedulerService iSchedulerService;

    @Autowired
    private DynamicSchedulerFactory dynamicSchedulerFactory;

    public void run(String... args) throws SchedulerException {
        iSchedulerService.removeJob("BatchInsertJob");
        DynamicJob BatchInsertJob = new DynamicJob("BatchInsertJob")
                .cronExpression("180")
                .target(BatchInsertJob.class);
        dynamicSchedulerFactory.registerJob(BatchInsertJob, new Date());
        log.error("注册调度任务BatchInsertJob [{}] -> ok", BatchInsertJob);
    }
}


